package org.cnbi.web.controller;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.velocity.tools.generic.NumberTool;
import org.cnbi.entity.Menu;
import org.cnbi.entity.User;
import org.cnbi.service.IHandleService;
import org.cnbi.utils.json.JsonUtil;
import org.cnbi.utils.pagination.PagingUtil;
import org.cnbi.utils.pojo.SqlGenerateBean;
import org.cnbi.utils.pojo.SqlParamsBean;
import org.cnbi.utils.pojo.SubjectSqlBean;
import org.cnbi.utils.tools.Constants;
import org.cnbi.utils.tools.MenuConstants;
import org.cnbi.web.controller.base.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
/**
 * forward 转发
 * redirect 重定向
 * Copyright © 2014中国经邦. All rights reserved.
 * @author 龚佳新
 * @date 2014年4月17日下午1:22:52
 * @cnbi-web
 */
@Controller
@RequestMapping("/main")
public class HandleController extends BaseController{
	
	private static final Log logger = LogFactory.getLog(HandleController.class);
	
	public static final NumberTool numTools = new NumberTool();
	

	
	@Autowired
	private  IHandleService handleService;
	
	/**
	 * 功能入口
	 * @param param 参数bean
	 * @param menu 菜单
	 * @param user 用户
	 * @param page 分页
	 * **/
	@RequestMapping(value="/main")  
	public ModelAndView index(SqlParamsBean param,Menu menu,User user,PagingUtil page,
				HttpServletRequest request,HttpServletResponse response) {
		   ModelAndView view = new ModelAndView();
		   logger.info("scode=="+menu.getScode()+"-->spcode=="+menu.getSpcode());
		   String spcode = menu.getSpcode();

		   view.addObject("pageBean", menu);
		   setContentType(response, null);
		   setContext(view);
		   view.setViewName(spcode);
		   Object datas = null;
		   if(spcode.equals(MenuConstants.TABLE)){
			   queryTable(view, menu, param);
		   }
		   
		   view.addObject(Constants.DATAS, datas);
		   logger.info("datas===>>>>"+JsonUtil.generateJson(datas));
		   view.addObject("numTools", numTools);
		return view;
	}
	
	

	/**
	 * 查询报表数据
	 * @param view
	 * @param menu
	 * @return
	 */
	private Object queryTable(ModelAndView view,Menu menu,SqlParamsBean param){
		    SubjectSqlBean subject = getSubjectSqlBeanByKey(param.getSubject());
		    SqlGenerateBean bean = new SqlGenerateBean(subject);
		    Map<String, String> conditionMap = param.getConditionMap();
		    String  itemValue = getDimValueStringByKeyAndSpcode(param.getShowdim()[0], menu.getScode());
		    conditionMap.put("item",itemValue);
		    bean.setConditionMap(conditionMap);
		    bean.setQueryFact(param.getQueryfact());
		    bean.setShowDim(param.getShowdim());
		    bean.setOrderByDim(new String[]{"DW_DimItem&ssortby"});
		    String sql = bean.getSql();
		    return  handleService.queryListMapBean(sql, null);
		   
	}
}
